package com.vegetable.modules.controller.inbound;

import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.inbound.AgentDelivery;
import com.vegetable.modules.entity.inbound.AgentDeliveryDetail;
import com.vegetable.modules.entity.inbound.AgentDeliveryReturn;
import com.vegetable.modules.entity.inbound.po.AgentDeliveryReturnPO;
import com.vegetable.modules.service.inbound.IAgentDeliveryReturnService;
import com.vegetable.modules.service.inbound.IAgentDeliveryService;
import com.vegetable.modules.service.inbound.IAgentDeliveryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 入库-代办交货退货 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-07
 */
@Slf4j
@RestController
@RequestMapping("/inStorage/agentDeliveryReturn")
@AllArgsConstructor
@Api(value = "入库代办交货退货", tags = "入库代办交货退货")
public class AgentDeliveryReturnController extends AbstractController {
    
    private IAgentDeliveryReturnService agentDeliveryReturnService;
    private IAgentDeliveryService agentDeliveryService;
    private IAgentDeliveryDetailService agentDeliveryDetailService;

    /**
     * 分页查询退货记录
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询退货记录", notes = "分页查询退货记录")
    public R page(@RequestBody AgentDeliveryReturnPO po) {
        try {
            return agentDeliveryReturnService.queryPage(po);
        } catch (Exception e) {
            log.error("分页查询退货记录失败", e);
            return R.fail("分页查询退货记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据订单ID查询订单详情（用于退货页面展示）
     */
    @GetMapping("/getDeliveryDetail")
    @ApiOperation(value = "根据订单ID查询订单详情", notes = "根据订单ID查询订单详情")
    public R getDeliveryDetail(@RequestParam Long deliveryId) {
        try {
            // 查询订单主表信息
            AgentDelivery delivery = agentDeliveryService.getById(deliveryId);
            if (delivery == null) {
                return R.fail("订单不存在");
            }
            
            // 查询订单明细信息
            List<AgentDeliveryDetail> details = agentDeliveryDetailService.selectByOrderId(deliveryId);
            delivery.setCommodities(details);
            
            return R.data(delivery);
        } catch (Exception e) {
            log.error("查询订单详情失败", e);
            return R.fail("查询订单详情失败: " + e.getMessage());
        }
    }

    /**
     * 保存退货信息
     */
    @PostMapping("/saveReturn")
    @ApiOperation(value = "保存退货信息", notes = "保存退货信息")
    public R saveReturn(@RequestBody AgentDeliveryReturn agentDeliveryReturn) {
        try {
            agentDeliveryReturn.setCreateUserId(getUserId());
            return agentDeliveryReturnService.saveReturn(agentDeliveryReturn);
        } catch (Exception e) {
            log.error("保存退货信息失败", e);
            return R.fail("保存退货信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据订单ID查询退货记录
     */
    @GetMapping("/getReturnsByDeliveryId")
    @ApiOperation(value = "根据订单ID查询退货记录", notes = "根据订单ID查询退货记录")
    public R getReturnsByDeliveryId(@RequestParam Long deliveryId) {
        try {
            List<AgentDeliveryReturn> returns = agentDeliveryReturnService.getReturnsByDeliveryId(deliveryId);
            return R.data(returns);
        } catch (Exception e) {
            log.error("查询退货记录失败", e);
            return R.fail("查询退货记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据退货ID查询退货详情
     */
    @GetMapping("/getReturnDetail")
    @ApiOperation(value = "根据退货ID查询退货详情", notes = "根据退货ID查询退货详情")
    public R getReturnDetail(@RequestParam Long returnId) {
        try {
            return agentDeliveryReturnService.getReturnDetail(returnId);
        } catch (Exception e) {
            log.error("查询退货详情失败", e);
            return R.fail("查询退货详情失败: " + e.getMessage());
        }
    }
}